package com.atguigu.order.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.nacosclass.service_edu.CourseOrderVO;
import com.atguigu.commonutils.nacosclass.service_ucenter.UcenterOrderVO;
import com.atguigu.order.client.OrderEduClient;
import com.atguigu.order.client.OrderUcenterClient;
import com.atguigu.order.entity.TOrder;
import com.atguigu.order.mapper.TOrderMapper;
import com.atguigu.order.service.TOrderService;
import com.atguigu.order.utils.OrderOnUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-07-10
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    private static final Logger log = LoggerFactory.getLogger(TOrderServiceImpl.class);
    @Autowired//获取课程信息
    private OrderEduClient orderEduClient;
    @Autowired//获取用户信息
    private OrderUcenterClient orderUcenterClient;
    @Override
    public String CreateOrders(String courseId, HttpServletRequest request) {
        //将课程信息获取
        CourseOrderVO courseOrderVO = orderEduClient.orderInfo(courseId);
        log.info("需要的课程信息courseOrderVO:{}",courseOrderVO);
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        log.info("需要的用户信息courseOrderVO:{}",memberIdByJwtToken);
        UcenterOrderVO orderIdMember = orderUcenterClient.getOrderIdMember(memberIdByJwtToken);
        TOrder order = new TOrder();
        order.setOrderNo(OrderOnUtil.getOrderNo());
        order.setCourseId(courseOrderVO.getId());
        order.setCourseCover(courseOrderVO.getCover());
        order.setCourseTitle(courseOrderVO.getTitle());
        order.setTotalFee(courseOrderVO.getPrice());
        order.setTeacherName(courseOrderVO.getTeacherName());
        order.setStatus(0);
        order.setPayType(1);
        log.info("需要的用户信息courseOrderVO:{}",order);
        order.setMemberId(orderIdMember.getId());
        order.setMobile(orderIdMember.getMobile());
        order.setNickname(orderIdMember.getNickname());
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
